//
//  ToolUnit.m
//  WeiXiao
//
//  Created by chenxiaojuan on 12-6-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ToolUnit.h"
#import "MBProgressHUD.h"

@implementation ToolUnit

static UIAlertView *sAlert = nil;

@synthesize HUD = _HUD;
@synthesize HUDCustom = _HUDCustom;

//SYNTHESIZE_SINGLETON_FOR_CLASS(ToolUnit);

+ (UIViewController *)loadFromNav:(NSString *)className{
    return [[UINavigationController alloc] initWithRootViewController:[ToolUnit loadFromVC:className]];
}

+ (UIViewController *)loadFromVC:(NSString *)className {
	UIViewController * newController = [[ NSClassFromString(className) alloc] init];
	//[newController autorelease];
	return newController;
}

+ (UIViewController *)loadFromNib:(NSString *)className {
	return [ToolUnit loadFromNib:className withClass:className];
}

+ (UIViewController *)loadFromNib:(NSString *)nibName withClass:(NSString *)className {
	UIViewController* newController = [[NSClassFromString(className) alloc]
									   initWithNibName:nibName bundle:nil];
	//[newController autorelease];
	
	return newController;
}

//字体的size
+ (CGSize)sizeOfText:(NSString *)text withFont:(UIFont *)font withWidth:(float) width{
    CGSize size =  [text sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    return size;
}


- (UIViewController*)loadFromNib:(NSString *)nibName withClass:className {
	UIViewController* newController = [[NSClassFromString(className) alloc]
									   initWithNibName:nibName bundle:nil];
	[newController autorelease];
	
	return newController;
}

- (UIViewController*)loadFromNib:(NSString*)className {
	return [self loadFromNib:className withClass:className];
}

- (UIViewController *)loadFromVC:(NSString *)className {
	UIViewController * newController = [[ NSClassFromString(className) alloc] init];
	[newController autorelease];
	return newController;
}

- (UIViewController*)loadTabBar:(NSString*)name{
    return nil;
}

#pragma mark - alert

+ (void)alert:(NSString*)title message:(NSString*)message
{
    if (sAlert) return;
    
    sAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(title,@"")
                                        message:NSLocalizedString(message,@"")
									   delegate:self
							  cancelButtonTitle:@"关闭"
							  otherButtonTitles:nil];
    [sAlert show];
    [sAlert release];
	sAlert = nil;
}

#pragma mark - MBProgressHUD

- (MBProgressHUD *) HUD{
	if (!_HUD) {
		_HUD = [[MBProgressHUD alloc] initWithView:[[UIApplication sharedApplication] keyWindow]];
		_HUD.animationType = MBProgressHUDAnimationZoom;
		_HUD.delegate = self;
		[[[UIApplication sharedApplication] keyWindow] addSubview:_HUD];
	}
    
	return _HUD;
}

//- (void)HUDShow:(NSString*)labelText yOffset:(NSString*)yOffset{
//	TT_RELEASE_SAFELY(_HUD);
//	self.HUD.labelText = labelText;
//	self.HUD.yOffset = [yOffset floatValue];
//	[self.HUD show:YES];
//}

- (void)HUDHide{
	[self.HUD hide:YES];
	[self.HUD performSelector:@selector(setMode:)
				   withObject:MBProgressHUDModeIndeterminate
				   afterDelay:0.5];
}

- (MBProgressHUD *) HUDCustom{
	if (!_HUDCustom) {
        _HUDCustom = [[MBProgressHUD alloc] initWithView:[[UIApplication sharedApplication] keyWindow]];
        [[[UIApplication sharedApplication] keyWindow] addSubview:_HUDCustom];
        
        _HUDCustom.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]] autorelease];
        
        _HUDCustom.mode = MBProgressHUDModeCustomView;
        _HUDCustom.delegate = self;
	}
    
	return _HUDCustom;
}

- (void)showWithCustomView:(NSString *)strTitle{
    
	self.HUDCustom.labelText = strTitle;
	
	[self.HUDCustom show:YES];
	[self.HUDCustom hide:YES afterDelay:1];
}


//时间格式-月日
+ (NSString *)monthDayFromTime:(NSString *)time{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:time];
    [formatter setDateFormat:@"MMMd"];
    NSString *monthDay = [formatter  stringFromDate:date];
    [formatter release];
    monthDay = [NSString stringWithFormat:@"%@日",monthDay];
    
    return monthDay;
    
}

//时间格式-HH:mm
+ (NSString *)hourSecondFromTime:(NSString *)time{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *date = [formatter dateFromString:time];
    [formatter setDateFormat:@"HH:mm"];
    NSString *hs = [formatter  stringFromDate:date];
    [formatter release];
    
    return hs;
}

//时间格式-yyyy年mm月dd日 上/下午HH:mm
+ (NSString *)yearMonthDayFromDate:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSString *str = [formatter stringFromDate:date];
    [formatter setDateFormat:@"MMMdd"];
    str = [NSString stringWithFormat:@"%@年%@日",str,[formatter stringFromDate:date]];
    [formatter setDateFormat:@"aaK:mm"];
    str = [NSString stringWithFormat:@"%@ %@",str,[formatter stringFromDate:date]];
    
    [formatter release];
    
    return str;
}

//时间格式-yyyy-MM-dd HH:mm
+ (NSString *)stringFromDate:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *str = [formatter stringFromDate:date];
    [formatter release];
    
    return str;
}

//时间格式-yyyy-MM-dd HH:mm:ss
+ (NSString *)stringFromDateLength:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *str = [formatter stringFromDate:date];
    [formatter release];
    
    return str;
}

//时间格式-NSDate
+ (NSDate *)dateFromString:(NSString *)time{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:time];
    
    return date;
}


//隐藏webview的阴影
+ (void)hiddenWebviewShaw:(UIWebView*)webview{
    UIScrollView *scrollview = [[webview subviews] objectAtIndex:0];
    if (scrollview) {
        for (UIView *v in [scrollview subviews]) {
            if ([v isKindOfClass:[UIImageView class]]) {
                v.hidden = YES;
            }
        }
    }
}

#pragma mark -
#pragma mark MBProgressHUDDelegate methods

- (void)hudWasHidden:(MBProgressHUD *)hud {
	// Remove HUD from screen when the HUD was hidded
	[_HUDCustom removeFromSuperview];
	[_HUDCustom release];
	_HUDCustom = nil;
}

#pragma mark -
#pragma mark - clear table catch data

//设置是否清除缓存的标志位
+ (void)setIsClearTableCatchData:(BOOL)blClear{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithBool:blClear] forKey:@"isClearTableCatch"];
    [defaults synchronize];
}

//设置是否清除缓存的标志位
+ (BOOL)getIsClearTableCatchData{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [[defaults objectForKey:@"isClearTableCatch"] boolValue];
}


+ (BOOL)checkEmpryOrWhitespace:(NSString *)text{
    return 0 == text.length ||
    ![text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length;
}





@end
